<?php

namespace app\index\controller;

use app\data\model\ActActivity;
use app\data\model\ActActivityDiy;
use app\data\model\ActTicketDetails;
use app\data\model\ActTicketGroup;
use app\data\model\ActUserModel;

class ActivityDetail extends BaseController
{
    protected function initialize()
    {
        parent::initialize();
        // $this->needLogin();
    }

    public function index($id)
    {
      
        $this->activity = ActActivity::withCount(['OrderModel'=>function($query,&$alias){
            $query->where('status',4);
            $alias = 'order_count';
        }])->where('id',$id)->append(['type_name','region_name'])->findOrFail();
        $this->theme  =  $this->activity->themeModel()->select()->append(['theme_name']);
        $this->guest  = $this->activity->guestModel()->select();
        $this->ticket = $this->activity->ticketModel()->append(['sale_status'])->findOrFail();
        $this->ticket_detail = ActTicketDetails::with(['ticketModel'])->where('activity_id',$id)->where('is_open',1)->append(['sale_status'])->select();
        $this->ticket_group  = ActTicketGroup::where('activity_id',$id)->append(['sale_status'])->where('group_is_open',1)->select();
        $this->diyform       = ActActivityDiy::where('activity_id',$id)->select();
        $this->creator       = ActUserModel::where('id',$this->activity->create_user_id)->findOrFail();

        $this->fetch();
    }

        /**
     * @name:门票检查
     * @author:Sam
     * @date:2024-04-14
     * @param:[string] param [desc]
    */
    public function check($id){
        $type = input('post.type');
        if($type == 1){
            //普通票
            if(!$ticket = ActTicketDetails::with(['ticketModel'])->where('id',$id)->append(['sale_status'])->find()) return $this->error('未找到票券');
            return $this->success('ok',$ticket);
        }elseif($type == 2){
            if(!$ticket = ActTicketGroup::where('id',$id)->append(['sale_status'])->find()) return $this->error('未找到票券');
            return $this->success('ok',$ticket);
        }else{
            return $this->error('数据异常');
        }
    }

}